<div class="modal-body pageContent">
	<div class="row">
        <div class="col-lg-12 col-md-12">
            
            <form action="{{url}}" enctype="multipart/form-data" method='post' id="userForm">
                <!-- <form id="orderForm" method="post" class="form-horizontal" action="/order/addOrder"> -->
                    {{#if id}}
                    <input type="hidden" name="id" value="{{id}}">
                    {{/if}}
                    <div class="form-group">
                    <label class="col-lg-12 control-label"><span class="indicator">*</span><span class="indicatorName">用户名</span></label>
                    <div class="col-lg-12">
                        <input type="text" class="form-control" name="name" value="{{name}}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-12 control-label">
                            {{#unless id}}
                            <span class="indicator">*</span>
                            {{/unless}}<span class="indicatorName">密码</span></label>
                    <div class="col-lg-12">
                        <input type="text" class="form-control" name="password" value="{{password}}" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-12 control-label">
                        <span class="indicator">*</span>
                        <span class="indicatorName">所属角色</span></label>
                    <div class="col-lg-12">
                        <select id="subordinateRole" class="selectpicker show-tick form-control" name="roleId">
                            <option value=""  selected  style="display:none">请选择所属角色</option>
                        </select>
                    </div>
                </div>
                
            </form>
        </div>
    </div>
</div>
<div class="modal-footer pageFooter">
    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
    <button  class="btn btn-primary"  type="submit" >保存</button>    
</div>

<script>
    $(function(){
        // console.log('{{roleId}}')
        
        
        $('.selectpicker').selectpicker();//初始化
        $('#userForm')
        .bootstrapValidator({
            message: '此值无效',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
             fields: {
                name: {
                    message: '请输入用户名称',
                    validators: {
                        notEmpty: {
                            message: '名称不能为空'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_-]{6,30}$/,
                            message: '用户名称只能由字母、数字、下划线和减号组成,长度必须大于6,小于30个字符'
                        }
                    }
                },
                roleId:{
                    validators: {
                        notEmpty: {
                            message: '请选择所属角色'
                        },
                    }
                },
            }
        })
        .on('success.form.bv', function(e) {
            // Prevent form submission
            e.preventDefault();
            // Get the form instance
            var $form = $(e.target);

            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');
            //console.log($form.serialize());
            // Use Ajax to submit form data 
            '{{id}}' ? $('#userListEidt').formAjax({'url':$form.attr('action'),'data':$form.serialize(),'tableRefresh':'#userTable'}) : $('#userListAdd').formAjax({'url':$form.attr('action'),'data':$form.serialize(),'tableRefresh':'#userTable'});
 
        });
        if(!'{{id}}'){
            $('#userForm').bootstrapValidator('addField','password',{
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    regexp: {
                        regexp: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{8,}/,
                        message: '不能纯数字或纯字母,长度至少为8位'
                    }
                }
            })
        }
        add_role_select()
    })
    function add_role_select(){
        $.ajax({
            type: 'post',
            url: '/user/getRoleList',
            success: function(result){
                if (result.length==9371) { //当跳转到登录页面
                       location.href ="/login";
                }else{
                    $.each(JSON.parse(result),function(i,item){
                        if('{{roleId}}'){
                            item.id == '{{roleId}}' ? $('#subordinateRole').append('<option selected value="'+item.id+'">'+item.name+'</optinon>') :  $('#subordinateRole').append('<option value="'+item.id+'">'+item.name+'</optinon>');
                        }else{
                            $('#subordinateRole').append('<option value="'+item.id+'">'+item.name+'</optinon>');
                        }
                    })
                    $('#subordinateRole').selectpicker('render');
                    $('#subordinateRole').selectpicker('refresh');
                }
            },
            error: function (result) {       //后台数据异常返回503
                if(result.status===503||result.status===500){ 
                    toastr.error("服务端数据异常"+'!');
                }
            } 
        })
    }
</script>